<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link href="../../../static/css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../../../static/js/jquery.js"></script>
    <script>
        var productId;
        var bra;
        var typ;
        var produc;
        var fir;
        $(function (){
            let pId = $("#pId").val();
           $.ajax({
               url:"/purchase/queryDetail2",
               type:"POST",
               dataType:"JSON",
               data:{pId:pId},
               success:function (data){
                   console.log(data);
                   let str=`<li>
      <label>采购单编号</label>
      <input name="" type="text" value="${data.purchaseId}" readonly="readonly" class="roinput" id="pId"/>
      <i>自动生成不能编辑</i>
    </li>
    <li>
      <label>采购时间</label>
      <input name="" type="text" value="${data.purchaseTime}" readonly="readonly" class="roinput" />
      <i>不能编辑</i>
    </li>
    <li>
      <label>创建人</label>
      <input name="" type="text" value="${data.uname}" readonly="readonly" class="roinput" />
      <i>不能编辑</i>
    </li>
    <li>
      <label>审核状态</label>
      <input type="text" value="${data.checkStatus}" readonly="readonly" class="roinput" />
      <input name="" type="hidden" value="1" />
      <i>初始状态，不能编辑</i>
    </li>
    <li>
      <label>总金额</label>
      <input name="" type="text" value="${data.totalMoney}" readonly="readonly" class="roinput" id="total2"/>
       <i>自动运算，不能编辑</i>
    </li>
    <li>
      <input type="button" value="新增" class="smallbtn" />
      &nbsp;&nbsp;&nbsp;&nbsp;
      <input type="button" value="删除" class="smallbtn" />
       &nbsp;&nbsp;&nbsp;&nbsp;
      <input type="button" value="保存" class="smallbtn" onclick="save()"/>
       &nbsp;&nbsp;&nbsp;&nbsp;
      <input type="button" value="返回" class="smallbtn" onclick="window.location.href='../../lists'"/>
    </li>`;
                   $("#ttt").html(str);

                   let str2 = `<thead>
        <tr>
          <th><input type="checkbox"/></th>
          <th>序号</th>
          <th>品牌</th>
          <th>类型</th>
          <th>型号</th>
          <th>厂商</th>
          <th>数量</th>
          <th>单位</th>
          <th>单价</th>
          <th>金额</th>
        </tr>
      </thead>
      <tbody id="pdu_tbody">
      \t<tr>
            <td><input type="checkbox"/></td>
          <td>1</td>
          <td>
          \t<select onclick="" id="first">
<!--            \t<option >请选择</option>-->
                <option selected="selected">${data.brandName}</option>
<!--                <option>海尔</option>-->
<!--                <option>小米</option>-->
            </select>
          </td>
          <td>
          \t<select id="second">
<!--            \t<option>请选择</option>-->
                <option selected="selected">${data.typeName}</option>
<!--                <option>台式电脑</option>-->
<!--                <option>手机</option>-->
            </select>
          </td>
          <td>
          \t<select id="third">
<!--            \t<option>请选择</option>-->
<!--                <option>X260</option>-->
<!--                <option>E470</option>-->
                <option selected="selected">${data.productModel}</option>
            </select>
          </td>
          <td>
          \t<select id="fourth">
<!--            \t<option>请选择</option>-->
                <option selected="selected">${data.firmName}</option>
<!--                <option>北京京东商贸股份有限公司</option>-->
            </select>
          </td>
          <td><input type="text" value="${data.count}" id="num"/></td>
          <td>${data.productUnit}</td>
          <td><input type="text" value="${data.purchasePrice}" id="price"/></td>
          <td><input type="text" value="${data.totalMoney}" id="total"/></td>
        </tr>
      </tbody>`;
                   $("#ttttt").html(str2);

                   secondAjax();
               },
               error:function (){}
           });


        });


        function secondAjax(){
            $.ajax({
                url:"/purchase/queryAllBrands",
                type:"POST",
                dataType: "JSON",
                data:{},
                success:function (brandList){
                     console.log(brandList);
                     firstBrand(brandList);
                },
                error:function (){}
            })
        }

        function firstBrand(brandList){
            // var number = $("#num").val();
             var price = $("#price").val();
            // var total = $("#total").val();
            var bbb = $("#first").val();
            var ttt = $("#second").val();
            var ppp = $("#third").val();
            var fff = $("#fourth").val();
            let str = ``;
            let str2 = ``;
            let str3 = ``;
            let str4 = ``
            for(let i=0;i<brandList.length;i++){
                 if(brandList[i].brandName==bbb){
                     str+=`<option selected="selected">${brandList[i].brandName}</option>`;
                     for(let j=0;j<brandList[i].types.length;j++){
                         if(brandList[i].types[j].typeName==ttt){
                             str2+=`<option selected="selected">${brandList[i].types[j].typeName}</option>`;
                             for(let k=0;k<brandList[i].types[j].products.length;k++){
                                 if(brandList[i].types[j].products[k].productModel==ppp){
                                     str3+=`<option selected="selected">${brandList[i].types[j].products[k].productModel}</option>`;
                                     str4 = `<option selected="selected">${brandList[i].types[j].products[k].firm.firmName}</option>`
                                 }else {
                                     str3 += `<option>${brandList[i].types[j].products[k].productModel}</option>`;
                                 }
                             }
                         }else {
                             str2 += `<option>${brandList[i].types[j].typeName}</option>`;
                         }
                     }
                 }else {
                     str += `<option>${brandList[i].brandName}</option>`;
                 }
            }
            $("#first").html(str);
            $("#second").html(str2);
            $("#third").html(str3);
            $("#fourth").html(str4);

            $("#first").blur(function (){
                var str5 = ``;
                var b5 = $("#first").val();
                if(bbb==b5){
                    return;
                }else {
                    bbb=b5;
                    for(let i=0;i<brandList.length;i++){
                        if(bbb==brandList[i].brandName){
                            for(let j=0;j<brandList[i].types.length;j++){
                                str5+=`<option>${brandList[i].types[j].typeName}</option>`;
                            }
                            str5+=`<option selected="selected">请选择</option>`
                            ttt = "请选择";
                            $("#second").html(str5);
                            $("#third").html(``);
                            $("#fourth").html(``);
                            $("#num").val(0);
                            $("#price").val(0);
                            $("#total").val(0);
                            $("#total2").val(0);
                            return;
                        }
                    }
                }
            });
            $("#second").blur(function (){
                var str6 = ``;
                var ttttt = $("#second").val();
                if(ttttt=="请选择"){
                    $("#third").html(``);
                    $("#fourth").html(``);
                    $("#num").val(0);
                    $("#price").val(0);
                    $("#total").val(0);
                    $("#total2").val(0);
                    return;
                }
                if(ttt==ttttt){
                    return;
                }else {
                    ttt = ttttt;
                    for(let i=0;i<brandList.length;i++){
                        if(brandList[i].brandName==bbb){
                            for(let j=0;j<brandList[i].types.length;j++){
                                if(brandList[i].types[j].typeName==ttt){
                                    for(let k=0;k<brandList[i].types[j].products.length;k++){
                                        str6+=`<option>${brandList[i].types[j].products[k].productModel}</option>`;
                                    }
                                    str6+=`<option selected="selected">请选择</option>`;
                                    ppp = "请选择";
                                    $("#third").html(str6);
                                    $("#fourth").html(``);
                                    $("#num").val(0);
                                    $("#price").val(0);
                                    $("#total").val(0);
                                    $("#total2").val(0);
                                    return;
                                }
                            }
                        }
                    }
                }
            });

            $("#third").blur(function (){
                var str7 = ``;
                var ppppp = $("#third").val();
                if(ppppp=="请选择"){
                    $("#fourth").html(``);
                    $("#num").val(0);
                    $("#price").val(0);
                    $("#total").val(0);
                    $("#total2").val(0);
                    return;
                }
                if(ppppp==ppp){
                    return
                }else {
                    ppp = ppppp;
                    for(let i=0;i<brandList.length;i++){
                        if(brandList[i].brandName==bbb){
                            for(let j=0;j<brandList[i].types.length;j++){
                                if(brandList[i].types[j].typeName==ttt){
                                    for(let k=0;k<brandList[i].types[j].products.length;k++){
                                        if(brandList[i].types[j].products[k].productModel==ppp){
                                            str7+=`<option selected="selected">${brandList[i].types[j].products[k].firm.firmName}</option>`;
                                            $("#price").val(brandList[i].types[j].products[k].productPrice);
                                            price = brandList[i].types[j].products[k].productPrice;
                                            productId = brandList[i].types[j].products[k].productId;
                                        }
                                    }
                                    $("#fourth").html(str7);
                                    return;
                                }
                            }
                        }
                    }
                }
            });

            $("#num").blur(function (){
                var number = $("#num").val();
                var total = number*price;
                $("#total").val(total);
                $("#total2").val(total);
            });
        }



        function save(){
            var count = $("#num").val();
            var purchaseId = $("#pId").val();
            var productPrice = $("#price").val();
            var totalMoney = $("#total").val();
            $.ajax({
                url:"/purchase/updateSaveee",
                type:"POST",
                dataType:"JSON",
                data:{count:count,purchaseId:purchaseId,productPrice:productPrice,totalMoney:totalMoney,productId:productId},
                success:function (flag){
                    if(flag==true){
                        alert("修改成功");
                        window.location.href='../../lists';
                    }else {
                        alert("修改失败");
                    }
                },
                error:function (){
                    alert("系统繁忙，请稍后再试");
                }
            });
        }
    </script>
</head>

<body>
<div class="place"> <span>位置：</span>
  <ul class="placeul">
    <li>采购管理</li>
    <li>采购单管理</li>
    <li>修改</li>
  </ul>
</div>
<div class="formbody">
  <div class="formtitle"><span>采购单信息</span></div>
  <ul class="forminfo" id="ttt">
    <li>
      <label>采购单编号</label>
      <input name="" type="text" th:value="${key}" readonly="readonly" class="roinput" id="pId"/>
      <i>自动生成不能编辑</i>
    </li>
    <li>
      <label>采购时间</label>
      <input name="" type="text" value="2017-11-18 15:36:10" readonly="readonly" class="roinput" />
      <i>不能编辑</i>
    </li>
    <li>
      <label>创建人</label>
      <input name="" type="text" value="马云" readonly="readonly" class="roinput" />
      <i>不能编辑</i>
    </li>
    <li>
      <label>审核状态</label>
      <input type="text" value="未审核" readonly="readonly" class="roinput" />
      <input name="" type="hidden" value="1" />
      <i>初始状态，不能编辑</i>
    </li>
    <li>
      <label>总金额</label>
      <input name="" type="text" value="9876582" readonly="readonly" class="roinput" />
       <i>自动运算，不能编辑</i>
    </li>
    <li>
      <input type="button" value="新增" class="smallbtn" />
      &nbsp;&nbsp;&nbsp;&nbsp;
      <input type="button" value="删除" class="smallbtn" />
       &nbsp;&nbsp;&nbsp;&nbsp;
      <input type="button" value="保存" class="smallbtn" />
       &nbsp;&nbsp;&nbsp;&nbsp;
      <input type="button" value="返回" class="smallbtn" onclick="window.location.href='purchaseList.html'"/>
    </li>
  </ul>
  <table class="tablelist" id="ttttt">
      <thead>
        <tr>
          <th><input type="checkbox"/></th>
          <th>序号</th>
          <th>品牌</th>
          <th>类型</th>
          <th>型号</th>
          <th>厂商</th>
          <th>数量</th>
          <th>单位</th>
          <th>单价</th>
          <th>金额</th>
        </tr>
      </thead>
      <tbody id="pdu_tbody">
      	<tr>
            <td><input type="checkbox"/></td>
          <td>1</td>
          <td>
          	<select>
            	<option>请选择</option>
                <option selected="selected">联想</option>
                <option>海尔</option>
                <option>小米</option>
            </select>
          </td>
          <td>
          	<select>
            	<option>请选择</option>
                <option selected="selected">笔记本电脑</option>
                <option>台式电脑</option>
                <option>手机</option>
            </select>
          </td>
          <td>
          	<select>
            	<option>请选择</option>
                <option>X260</option>
                <option>E470</option>
                <option selected="selected">T470</option>
            </select>
          </td>
          <td>
          	<select>
            	<option>请选择</option>
                <option selected="selected">北京联想科技股份有限公司</option>
                <option>北京京东商贸股份有限公司</option>
            </select>
          </td>
          <td><input type="text" value="10" /></td>
          <td>台</td>
          <td><input type="text" value="9998" /></td>
          <td><input type="text" value="99980" /></td>
        </tr>

      </tbody>
  </table>
</div>
<script type="text/javascript">
	$('.tablelist tbody tr:odd').addClass('odd');
</script>
</body>
</html>
